//
//  SendAutoModel.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/21.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  发送验证码模型

import UIKit



class SendAutoModel: NSObject {
    static let shared = SendAutoModel()
    
    //闭包
    var comfun:((_ _data : String) -> Void)?

    var aa : String = ""
    
    
}


extension SendAutoModel {
    /**
     ## 发送忘记密码验证码
     封装发送验证码业务逻辑
     
     - tfNum    电话号码
     */
    func sendForgetAuto(sendType : Int,tfNum : UITextField,_com:@escaping (_ _data:String)->Void) -> Void {
        
        //闭包内存传值
        self.comfun = _com
        
        //检查验证码是否为空
        if tfNum.text?.characters.count == 0 {
//            MBManager.showBriefAlert(tfNumIsNull)
            CustomAlertView.shared.alertWithTitle(strTitle: tfNumIsNull)
//            return
        } else if Encryption.checkTelNumber(tfNum.text) == false {
            
//            MBManager.showBriefAlert(tfNumNotCor)
            CustomAlertView.shared.alertWithTitle(strTitle: tfNumNotCor)
//            return
        } else if Encryption.checkTelNumber(tfNum.text) == true {
            
            let params = ["type": sendType,
                          "phone":tfNum.text!] as [String : Any]
            print(params)
            
            aa = "true"
            
            self.comfun!(aa)
            
//            MBManager.showPermanentMessage("发送中...", in: nav?.view)
            
            CustomAlertView.shared.alertAfterDown(strTitle: "发送中...")
            postWithPath(path: sendMsgUrl, paras: params, success: { (response) in
                
                //判读返回值是否为空
                guard let dic = response as? NSDictionary else {

                    return
                }
                
                print(dic)
                
                
                //提取提示语
                let alertmsg = dic["resultmsg"] as! String
                
                if alertmsg == "手机号已被注册" {
                    CustomAlertView.shared.dissmiss()
                    
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: { 
                        CustomAlertView.shared.alertWithTitle(strTitle: numUsed)
                    })
                    return
                } else if alertmsg == "成功" {
//                    MBManager.hideAlert()
//                    MBManager.showBriefAlert(msgSended)
                    
                    CustomAlertView.shared.dissmiss()
                    
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
                        CustomAlertView.shared.alertWithTitle(strTitle: msgSended)
                    })
                } else if alertmsg == "触发业务流控" {
//                    MBManager.hideAlert()
//                    MBManager.showBriefAlert(msgSengWrong)
                    CustomAlertView.shared.dissmiss()
                    
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
                        CustomAlertView.shared.alertWithTitle(strTitle: msgSengWrong)
                    })
                    
                } else if alertmsg == "发送短信失败" {
//                    MBManager.hideAlert()
//                    MBManager.showBriefAlert(msgSengWrong)
                    CustomAlertView.shared.dissmiss()

                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
                        CustomAlertView.shared.alertWithTitle(strTitle: msgSengWrong)
                    })
                }
                
                //接收返回值
                self.comfun!(alertmsg)
                
            }, failure: { (error) in
//                print(error.localizedDescription)
//                self.comfun!(error.localizedDescription)
                
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: {
                    CustomAlertView.shared.alertWithTitle(strTitle: netWrong)
                })
            })
   
        
        }
        
        CustomAlertView.shared.alertWithTitle(strTitle: netWrong)

    }
}
